package com.jk.smartelderlycare.controller;

import com.jk.smartelderlycare.dto.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author hhk
 * @version 1.0
 * @ClassName HelloSenderController
 * @program: smart-elderly-care
 * @description: TODO
 * @date 2024/11/28 14:23
 */
@Api(tags = "消息队列生产者测试")
@Slf4j
@RestController
public class HelloSenderController {
    @Autowired
    private AmqpTemplate amqpTemplate;
    @ApiOperation(value = "发送消息", httpMethod = "GET", notes = "发送消息")
    @GetMapping(value = "send")
    public String send(){
        for (int i = 0; i < 1000; i++) {
            String context = "hello " + new Date();
            System.out.println("发送的消息 : " + context);
            amqpTemplate.convertAndSend("hello", context);
        }
    return "发送成功";
    }
    @ApiOperation(value = "高级消息发送Object", httpMethod = "POST", notes = "高级消息发送Object")
    @PostMapping(value = "sendObject")
    private String sendObject(@RequestBody SysUser sysUser){
        log.info("sendObject : " + sysUser.toString());
        amqpTemplate.convertAndSend("object", sysUser);
        return "发送成功";
    }
    @ApiOperation(value = "topicexchange交换器转发消息", httpMethod = "POST", notes = "topicexchange交换器转发消息")
    @PostMapping(value = "sendMessageByExchange")
    private String sendMessageByExchange(@RequestBody SysUser sysUser){
        System.out.println("发送消息topicExchange" + sysUser.toString());
                                    //  交换器          队列名           发送对象
        amqpTemplate.convertAndSend("exchange", "topic.message", sysUser);

        amqpTemplate.convertAndSend("exchange", "topic.messages", sysUser);
        return "发送成功!!!";
    }
    @ApiOperation(value = "fanoutExchange交换器转发消息", httpMethod = "POST", notes = "fanoutExchange交换器转发消息")
    @PostMapping(value = "sendMessageFanoutExchange")
    public String sendMessageFanoutExchange(@RequestBody SysUser sysUser) {
        System.out.println("发送消息fanoutExchange : " + sysUser.toString());
                                        //没有指定的队列 所有和 fanoutExchange 交换器绑定的队列都会收到
        amqpTemplate.convertAndSend("fanoutExchange", "", sysUser);
        return "发送成功!!!";
    }


















}
